package org.chenzhi.mvp.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.v7.app.AppCompatActivity;
import android.util.SparseArray;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

/**
 * Template of AppCompatActivity *
 * @author Zhi Chen
 */

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {
    private SparseArray<View> mViews; //create view cache

    public abstract int getLayoutId(); //get layout's id

    //region init operations, these will be done on activity start,first view,then data
    public void init() {
    }

    //endregion
    public void processClick(int resId) {} //deal on-click event

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViews = new SparseArray<>();
        setContentView(getLayoutId());

        init();
    }

    @CallSuper
    protected void onDestroy() {
        super.onDestroy();

        if (mViews != null) {
            mViews.clear();
            mViews = null;
        }
    }

    /**
     * get view
     *
     * @param resId id of the need view
     * @return view
     */
    public <E extends View> E findView(int resId) {
        E view = (E) mViews.get(resId);
        if (view == null) {
            try {
                view = (E) findViewById(resId);
                mViews.put(resId, view);
            } catch (Exception ignored) {
            }
        }
        return view;
    }

    /**
     * Full screen and hide status bar
     */
    public void hideStatusBar() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        View view = getWindow().getDecorView();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
        } else {
            view.setSystemUiVisibility(View.INVISIBLE);
        }
    }

    public void hideNavigationBar() {
        View view = getWindow().getDecorView();
        view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
    }

    public void setText(int resId, String text) {
        TextView textView = findView(resId);
        textView.setText(text);
    }

    public String getContent(int resId) {
        TextView textView = findView(resId);
        return textView.getText().toString();
    }

    //region click event code block

    /**
     * trigger click event
     *
     * @param ids id of all views which need trigger click event
     */
    public void setClick(int... ids) {
        for (int id : ids) {
            findView(id).setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View view) {
        processClick(view.getId());
    }
    //endregion
}
